c# and linq: want {1,1,2,3} == {1,2,3,1} returns true but {1,1,2,3} == {1,2,3} returns false

Posted by dFlat on Stack Overflow See other posts from Stack Overflow or by dFlat
Published on 2011-01-02T01:54:49Z Indexed on 2011/01/02 2:54 UTC
Read the original article Hit count: 368

Filed under:
|
|

I have two sets, both IEnumerables, and I want to compare them.

string[] names1 = { "tom", "dick", "harry" };
string[] names2 = { "tom", "dick", "harry", "harry"};
string[] names3 = { "tom", "dick", "harry", "sally" };
string[] names4 = { "dick", "harry", "tom" };

Want names1 == names4 to return true (and self == self returns true obviously)
But all other combos return false.

What is the most efficient way? These can be large sets of complex objects.

I looked at doing:
var a = name1.orderby<MyCustomType, string>(v => v.Name);
var b = name4.orderby<MyCustomType, string>(v => v.Name);

return a == b;

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ